package com.xwj.service.Impl;

import com.xwj.dao.RaMenuDao;
import com.xwj.entity.RaMenuInfo;
import com.xwj.entity.RaRoot;
import com.xwj.service.Interface.RaMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by thinkjoy on 2017/7/10.
 */
@Slf4j
@Service
@CacheConfig(cacheNames = "RaMenuCache")
public class RaMenuServiceImpl implements RaMenuService {

    @Autowired
    private RaMenuDao raMenuDao;
    @Autowired
    private RaRootServiceImpl raRootService;

    /**
     * 找出所有菜单并按照父子关系拼接
     *
     * @return
     */
    @Override
    @Cacheable(key = "#root.methodName")
    public List<RaMenuInfo> findItemMenu() {
        List<RaMenuInfo> pMenu = raMenuDao.findItemMenu(0);//找出父级菜单
        List<RaMenuInfo> raMenuInfos = new ArrayList<>();
        for (RaMenuInfo raMenuInfo : pMenu) {
            RaRoot raRoot = raRootService.findRootById(raMenuInfo.getRootId());
            if (raRoot != null)
                raMenuInfo.setRootName(raRoot.getRoot());
            List<RaMenuInfo> itemmenus = raMenuDao.findItemMenu(raMenuInfo.getId());//找出所有的子菜单
            if (itemmenus.size() > 0) {
                List<RaMenuInfo> items = new ArrayList<>();
                itemmenus.forEach(item -> {
                    RaRoot itemRoot = raRootService.findRootById(item.getRootId());
                    if (itemRoot != null)
                        item.setRootName(itemRoot.getRoot());
                    items.add(item);
                });
                raMenuInfo.setItemmenus(items);
            }
            raMenuInfos.add(raMenuInfo);
        }
        return raMenuInfos;
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public int updateMenu(RaMenuInfo raMenuInfo) {
        try {
            return raMenuDao.updateMenu(raMenuInfo);
        } catch (Exception e) {
            log.error("RaMenuService异常:", e);
            return 0;
        }
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public int deleteMenu(int id) {
        try {
            return raMenuDao.deleteMenu(id);
        } catch (Exception e) {
            log.error("RaMenuService异常:", e);
            return 0;
        }
    }

    @Override
    @Cacheable(key = "#root.methodName + #id")
    public List<RaMenuInfo> findMenuByRootId(int id) {
        try {
            return raMenuDao.findMenuByRootId(id);
        } catch (Exception e) {
            log.error("RaMenuService异常:", e);
            return Collections.emptyList();
        }
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public int addMenuByMenuInfo(RaMenuInfo raMenuInfo) {
        try {
            return raMenuDao.addMenuByMenuInfo(raMenuInfo);
        } catch (Exception e) {
            log.error("RaMenuService异常:", e);
            return 0;
        }
    }

}
